package com.mercenary.wein.screenControl.command.jobSchedual;

import com.mercenary.wein.screenControl.bean.CommandBean;
import com.mercenary.wein.screenControl.bean.ScheduleTask;
import com.mercenary.wein.screenControl.command.CommandPool;
import com.mercenary.wein.screenControl.service.CommandService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tuxiao on 2017/3/25.
 */
public class InitJob {
    static Log log = LogFactory.getLog(InitJob.class);
    private static CommandService commandService = new CommandService();

    public static void initJob() {
        log.info("=====================================================================\n" +
                "|                          开始初始化调度任务                         |\n" +
                "=====================================================================");
        List<ScheduleTask> scheduleTasks = commandService.getScheduleTask();
        if(scheduleTasks!=null){
            List<CommandBean> commandBeens = new ArrayList<CommandBean>();
            for (ScheduleTask task : scheduleTasks) {
                try {
                    CommandBean commandBean = task.getCommandBean();
                    if (commandBean.getExeModeBean().getMode() == 1) {
                        if (!ifDelay(commandBean.getExeModeBean().getExeDate())){
                            commandBeens.add(commandBean);
                        }else {
                            commandService.deleteSchedualTask(task.getId());
                        }
                    }else {
                        commandBeens.add(commandBean);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            CommandPool.sendCommand(commandBeens);
        }

    }

    private static boolean ifDelay(Date time) {
        return time.getTime() < new Date().getTime();
    }

    public static void main(String[] args) {
        InitJob.initJob();
    }
}
